package com.bnq.control.common.widget;

import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;

import androidx.appcompat.widget.AppCompatTextView;


public class ProgressIndicator extends AppCompatTextView {

    public ProgressIndicator(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public ProgressIndicator(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ProgressIndicator(Context context) {
        super(context);
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        Drawable[] ds = getCompoundDrawables();
        Drawable d;
        if (ds.length > 0) {
            for (Drawable d1 : ds) {
                d = d1;
                if (d instanceof AnimationDrawable) {
                    ((AnimationDrawable) d).start();
                }
            }
        }
        Drawable drawable = getBackground();
        if (null != drawable && drawable instanceof AnimationDrawable) {
            ((AnimationDrawable) drawable).start();
        }
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        Drawable[] ds = getCompoundDrawables();
        Drawable d;
        if (ds.length > 0) {
            for (Drawable d1 : ds) {
                d = d1;
                if (d instanceof AnimationDrawable) {
                    ((AnimationDrawable) d).stop();
                }
            }
        }
        Drawable drawable = getBackground();
        if (null != drawable && drawable instanceof AnimationDrawable) {
            ((AnimationDrawable) drawable).stop();
        }
    }
}
